package com.yhxd.modular.reference.controller;

import com.yhxd.modular.reference.entity.Duties;
import com.yhxd.modular.reference.service.DutiesService;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/ref/duties")
@Api
public class DutiesController {

    @Autowired
    private DutiesService dutiesService;

    /**
     * 跳转到编辑页
     * @return
     */
    @GetMapping(value = "/toEdit")
    public String toEdit(Model model) {
        Duties duties = this.dutiesService.findById(1L);
        model.addAttribute("duties", duties);
        return "mng/reference/duties_edit";
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = "/edit")
    @RequiresPermissions("duties_edit")
    @ResponseBody
    public ResultVo edit(Duties duties) {
        try {
            this.dutiesService.saveOrUpdate(duties);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 跳转到展示页
     * @return
     */
    @PostMapping(value = "/info")
    public String dutiesInfo(Model model) {
        Duties duties = this.dutiesService.findById(1L);
        model.addAttribute("duties", duties);
        return "web/pages/duties_info";
    }
}
